package com.xiangyang.pos.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiangyang.pos.R;
import com.xiangyang.pos.bean.EmployeeDataBean;
import com.xiangyang.pos.constant.AppConstant;
import com.xiangyang.pos.http.HttpClient;
import com.xiangyang.pos.http.RequestParameters;
import com.xiangyang.pos.utils.SharedUtils;

/**
 * Created by Administrator on 2017/8/17 0017.
 *
 * 登录界面
 *
 * @author YeDalin
 *
 */

public class EmployeeLoginActivity extends Activity {

    private EditText etAccount;
    private EditText etPassword;
    private Button login;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_employee_login);
        initView();
        setMyClick();
    }

    private void initView(){
        etAccount=findViewById(R.id.et_account);
        etPassword=findViewById(R.id.et_password);
        login=findViewById(R.id.login);
    }

    private void setMyClick(){
        login.setOnClickListener(listener);
    }

    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.login:
                    //调用网络判断是否登录
                    if(isAccountOrPasswordEmpty()){

//                        没有网络接口，暂时直接跳转
                        /*Intent intent=new Intent(EmployeeLoginActivity.this,FirstActivity.class);
                        startActivity(intent);
                        finish();*/

                        setLogin();


                    }
//                    setLogin();
                    break;
            }
        }
    };


//    获取账号密码
    private RequestParameters getEmployeeData() {
        RequestParameters requestParameters = new RequestParameters();

//        String password= CryptoUtil.Md5(etPassword.getText().toString().trim());

        requestParameters.put("username", etAccount.getText().toString().trim() + "");
        requestParameters.put("password", etPassword.getText().toString().trim() + "");
        return requestParameters;
    }

    private final String LOGIN_URL= AppConstant.POS_URL+"/User/An/checkLogin";

//    连接网络
    private void setLogin(){

        HttpClient httpClient=new HttpClient();
        httpClient.post(LOGIN_URL, getEmployeeData(), new HttpClient.OnResponseListener() {
            @Override
            public void onResponse(String result) {
                if(!result.equals("Error")){

                    Gson gson=new Gson();

                    EmployeeDataBean bean=gson.fromJson(result,EmployeeDataBean.class);

                    if(bean!=null){
                        if(bean.getStatus()==1){
                            setData(bean);
                            Intent intent=new Intent(EmployeeLoginActivity.this,FirstActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            Toast.makeText(EmployeeLoginActivity.this, "账号或密码不正确", Toast.LENGTH_SHORT).show();
                        }
                    }
                }else {
                    Toast.makeText(EmployeeLoginActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


//    判断账号、密码是否为空
    private boolean isAccountOrPasswordEmpty(){
        if(etAccount.getText().length()==0){
            Toast.makeText(EmployeeLoginActivity.this, "账号不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (etPassword.getText().length()==0){
            Toast.makeText(EmployeeLoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

//    存储数据
    private void setData(EmployeeDataBean bean){

        SharedUtils.setStringPrefs(EmployeeLoginActivity.this,"name",bean.getData().getName());
        SharedUtils.setStringPrefs(EmployeeLoginActivity.this,"address",bean.getData().getAddress());
        SharedUtils.setStringPrefs(EmployeeLoginActivity.this,"apiToken",bean.getApiToken());
        SharedUtils.setStringPrefs(EmployeeLoginActivity.this,"UserTOKEN",bean.getUserTOKEN());
        SharedUtils.setStringPrefs(EmployeeLoginActivity.this,"account",etAccount.getText().toString());
        SharedUtils.setStringPrefs(EmployeeLoginActivity.this,"shopId",bean.getData().getShop_id());
        SharedUtils.setStringPrefs(EmployeeLoginActivity.this,"employeeId",bean.getData().getId());
    }


}
